package org.tis.tools.abf.module.sys.controller;

import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tis.tools.abf.module.sys.controller.request.*;
import org.tis.tools.abf.module.sys.entity.SysDict;
import org.tis.tools.abf.module.sys.entity.enums.DictType;
import org.tis.tools.abf.module.sys.service.ISysDictService;
import org.tis.tools.core.web.controller.BaseController;
import org.tis.tools.core.web.vo.SmartPage;
import org.tis.tools.model.common.ResultVO;
import org.tis.tools.model.log.enums.OperateType;
import org.tis.tools.starter.operatelog.annotation.OperateLog;

import javax.validation.constraints.NotBlank;
import java.util.List;

@RestController
@RequestMapping("/sysDicts")
@Validated
@Api(description = "业务字典")
@Slf4j
public class SysDictController  extends BaseController {
    @Autowired
    private ISysDictService iSysDictService;

    @OperateLog(type = OperateType.ADD, desc = "新增业务字典")
    @ApiOperation(value = "新增业务字典", notes = "传入SysDictRequest类作为参数")
    @PostMapping
    public ResultVO createSysDict(@RequestBody @Validated @ApiParam(value = "SysDictRequest",name = "传入的SysDictRequest类", required = true) SysDictRequest request) {

        if (iSysDictService.existAddDictKey(request.getDictKey())){
            return ResultVO.error("404", "该业务字典已经存在,请重新输入！");
        }

        SysDict sysDict = new SysDict();
        sysDict.setDictKey(request.getDictKey());
        sysDict.setDictType(request.getDictType());
        sysDict.setDictName(request.getDictName());
        sysDict.setDictDesc(request.getDictDesc());
        sysDict.setGuidParents(request.getGuidParents());
        sysDict.setDefaultValue(request.getDefaultValue());
        sysDict.setFromTable(request.getFromTable());
        sysDict.setUseForKey(request.getUseForKey());
        sysDict.setUseForName(request.getUseForName());
        sysDict.setSeqno(request.getSeqNo());
        sysDict.setSqlFilter(request.getSqlFilter());
        sysDict.setFromType(request.getFromType());
        sysDict.setSendValue(request.getSendValue());
        sysDict =  iSysDictService.addDict(sysDict);
        return ResultVO.success("添加成功",sysDict);
    }


    @OperateLog(type = OperateType.DELETE, desc = "删除业务字典")
    @ApiOperation(value = "删除业务字典", notes = "根据guid删除对应的数据信息")
    @DeleteMapping("/{id}")
    public ResultVO deleteSysDict(@PathVariable @NotBlank(message = "id不能为空")  @ApiParam(value = "id",name = "业务字典GUID", required = true) String id) {
        SysDict sysDict = iSysDictService.selectById(id);

        if (sysDict == null){
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }else if (sysDict.getDictType().equals(DictType.SYS)){
            return ResultVO.error("404", "系统级字典不允许删除！");
        }

        iSysDictService.deleteDict(id);
        return ResultVO.success("删除成功",sysDict);
    }


    @OperateLog(type = OperateType.DELETE, desc = "批量删除业务字典")
    @ApiOperation(value = "批量删除业务字典",notes = "传入SysDictDelRequest类作为参数")
    @PostMapping("/deleteSysDictList")
    public ResultVO deleteSysDictList(@RequestBody @Validated @ApiParam(value = "SysDictDelRequest",name = "传入的SysDictDelRequest类", required = true) SysDictDelRequest sysDictDelRequest){
        return ResultVO.success("删除成功",iSysDictService.deleteDictList(sysDictDelRequest));
    }


    @OperateLog(type = OperateType.UPDATE, desc = "修改业务字典")
    @ApiOperation(value = "修改业务字典", notes = "传入SysDictRequest类作为参数")
    @PutMapping
    public ResultVO updateSysDict(@RequestBody @Validated @ApiParam(value = "SysDictRequest",name = "传入的SysDictRequest类", required = true) SysDictRequest request) {

        SysDict sysDictQue = iSysDictService.selectById(request.getGuid());
        if (sysDictQue == null){
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        if (iSysDictService.existUpdateDictKey(request.getDictKey(),request.getGuid())){
            return ResultVO.error("404", "该业务字典已经存在,请重新输入！");
        }

        SysDict sysDict = new SysDict();
        sysDict.setGuid(request.getGuid());
        sysDict.setDictKey(request.getDictKey());
        sysDict.setDictType(request.getDictType());
        sysDict.setDictName(request.getDictName());
        sysDict.setDictDesc(request.getDictDesc());
        sysDict.setGuidParents(request.getGuidParents());
        sysDict.setDefaultValue(request.getDefaultValue());
        sysDict.setFromTable(request.getFromTable());
        sysDict.setUseForKey(request.getUseForKey());
        sysDict.setUseForName(request.getUseForName());
        sysDict.setSeqno(request.getSeqNo());
        sysDict.setSqlFilter(request.getSqlFilter());
        sysDict.setFromType(request.getFromType());
        sysDict.setSendValue(request.getSendValue());
        iSysDictService.editSysDict(sysDict);
        return ResultVO.success("修改成功",sysDict);
    }


    @ApiOperation(value = "查询业务字典", notes = "根据guid查询出对应的数据信息")
    @GetMapping("/{id}")
    public ResultVO querySysDict(@PathVariable @NotBlank(message = "业务字典GUID不能为空")  @ApiParam(value = "id",name = "业务字典GUID", required = true) String id) {
        SysDict sysDict = iSysDictService.querySysDictByGuid(id);
        if (sysDict == null){
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("查询成功",sysDict);
    }


    @ApiOperation(value = "分页根据条件(dictKey)模糊查询所有业务字典", notes = "需要分页(page)查询参数(condition)传入")
    @PostMapping ("/list")
    public ResultVO querySysDictList(@RequestBody @Validated @ApiParam(value = "page",name = "分页信息", required =
            true) SmartPage<SysDict> page){
        Page<SysDict> sysDict = iSysDictService.querySysDicts(page.getPageObj(), page.getConditionWrapper());
        return ResultVO.success("查询成功",sysDict);
    }


    @ApiOperation(value = "查询所有的父业务字典",notes = "不需要传入参数")
    @GetMapping("/ParentList")
    public ResultVO queryParentList(){
        List<SysDict> list = iSysDictService.queryParentList();
        return ResultVO.success("查询成功",list);
    }

    @ApiOperation(value = "dictKey和dictName查询", notes = "传入SysDictQueryRequest类作为参数")
    @PostMapping("/lists")
    public ResultVO dictKeyOrQuery(@RequestBody @Validated @ApiParam(value = "SysDictQueryRequest",name = "传入的SysDictQueryRequest类", required = true) SysDictQueryRequest request) {
        SysDict sysDict = new SysDict();
        sysDict.setDictKey(request.getDictKey());
        sysDict.setDictName(request.getDictName());
        return ResultVO.success("查询成功",iSysDictService.dictKeyQuery(sysDict));
    }


    @ApiOperation(value = "查询业务字典树", notes = "传入父字典GUID")
    @PostMapping("/tree/{guid}")
    public ResultVO tree(@PathVariable @NotBlank(message = "guid不能为空") @ApiParam(value = "guid",name = "业务字典的GUID", required = true) String guid){
        SysDict sysDict = iSysDictService.selectById(guid);
        if (sysDict == null){
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("查询成功",iSysDictService.queryDictTree(guid));
    }

    @ApiOperation(value = "查询业务字典树", notes = "传入SysDictFilterQueryRequest类作为参数")
    @PostMapping("/treeFilter")
    public ResultVO treeFilter(@RequestBody @Validated @ApiParam(value = "SysDictFilterQueryRequest",name = "传入的SysDictFilterQueryRequest类", required = true) SysDictFilterQueryRequest filterQueryRequest){
        SysDict sysDict = iSysDictService.selectById(filterQueryRequest.getGuid());
        if (sysDict == null){
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("查询成功",iSysDictService.filterQueryDictTree(filterQueryRequest));
    }

    @ApiOperation(value = "设置默认字典项",notes = "传入SysDictDefaultValueRequest类作为参数")
    @OperateLog(type =OperateType.UPDATE,desc = "设置默认字典项")
    @PutMapping("/setDefaultValue")
    public ResultVO setDefaultValue(@RequestBody @Validated @ApiParam(value = "SysDictDefaultValueRequest",name = "传入的SysDictDefaultValueRequest类", required = true) SysDictDefaultValueRequest sysDictDefaultValueRequest){
        SysDict sysDict = iSysDictService.selectById(sysDictDefaultValueRequest.getGuid());
        if (sysDict == null){
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }

        sysDict = iSysDictService.setDefaultValue(sysDictDefaultValueRequest);
        return ResultVO.success("设置默认字典项成功!",sysDict);
    }

    @ApiOperation(value = "根据key值查询业务字典",notes = "传入业务字典的key(dictKey)作为参数")
    @GetMapping("/queryDictByKey/{dictKey}")
    public ResultVO queryDictByKey(@PathVariable @NotBlank(message = "key值不能为空") @ApiParam(value = "dictKey",name = "业务字典的key", required = true) String dictKey){
        return ResultVO.success("查询成功",iSysDictService.queryDictByKey(dictKey));
    }

    @ApiOperation(value = "查询除了该业务字典的其他业务字典集合",notes = "传入业务字典的GUID(id)作为参数")
    @GetMapping("/queryDictExcept/{id}")
    public ResultVO queryDictExcept(@PathVariable @NotBlank(message = "GUID不能为空") @ApiParam(value = "id",name = "业务字典的GUID", required = true) String id){
        return ResultVO.success("查询成功",iSysDictService.queryDictExcept(id));
    }

    @ApiOperation(value = "新增时该业务字典KEY是否重复",notes = "传入业务字典的key(dictKey)作为参数")
    @PostMapping("/existAddDictKey")
    public ResultVO getAddDictKey(@RequestBody @Validated @ApiParam(value = "SysDictKeyRequest",name = "传入的SysDictKeyReqyest类", required = true) SysDictKeyRequest sysDictKeyRequest){
        return ResultVO.success("查询成功",iSysDictService.existAddDictKey(sysDictKeyRequest.getDictKey()));
    }

    @ApiOperation(value = "修改时该业务字典KEY是否重复",notes = "传入业务字典的key(dictKey)和GUID(guid)作为参数")
    @PostMapping("/existUpdateDictKey")
    public ResultVO getUpdateDictKey(@RequestBody @Validated @ApiParam(value = "SysDictKeyRequest",name = "传入的SysDictKeyReqyest类", required = true) SysDictKeyRequest sysDictKeyRequest){
        SysDict sysDict = iSysDictService.selectById(sysDictKeyRequest.getGuid());
        if (sysDict == null){
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("查询成功",iSysDictService.existUpdateDictKey(sysDictKeyRequest.getDictKey(),
                sysDictKeyRequest.getGuid()));
    }
}
